package com.gestocasa.view.dialogs.helper;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class DatePicker extends JPanel {
	
	private JLabel l;
	private String day;
	private JButton[] button;
	private int month;
	private int year;
	private Calendar selectedDate;
	private boolean fastMode;
	
	public DatePicker(Date date, boolean mode) {
		super();
		
		l = new JLabel("", JLabel.CENTER);
		day = "";
		button = new JButton[49];
		month = java.util.Calendar.getInstance().get(java.util.Calendar.MONTH);
		year = java.util.Calendar.getInstance().get(java.util.Calendar.YEAR);
		
		selectedDate = Calendar.getInstance(TimeZone.getDefault(), Locale.getDefault());
		if(date != null) {
			selectedDate.setTime(date);
			month = selectedDate.get(Calendar.MONTH);
			year = selectedDate.get(Calendar.YEAR);
		}
		fastMode = mode;
		
		String[] header = { "Do", "Lu", "Ma", "Mi", "Ju", "Vi", "Sa" };
		JPanel p1 = new JPanel(new GridLayout(7, 7));
		JPanel p2 = new JPanel(new GridLayout(1, 3));
		JButton previous = new JButton("<<");
		JButton next = new JButton(">>");
		setLayout(new BorderLayout());
		
		previous.addActionListener(new ActionListener() {
		    public void actionPerformed(ActionEvent ae) {
	            month--;
	            displayDate();
		    }
		});
		
		next.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                month++;
                displayDate();
            }
	    });
		
		for (int x = 0; x < button.length; x++) {
	        final int selection = x;
	        button[x] = new JButton();
	        button[x].setFocusPainted(false);
	        button[x].setBackground(Color.white);
	        if (x > 6)
	        	button[x].addActionListener(new ActionListener() {
	                public void actionPerformed(ActionEvent ae) {
	                	day = button[selection].getActionCommand();
	                	if(day.isEmpty() == false) {
	                		selectedDate.set(Calendar.DAY_OF_MONTH, Integer.valueOf(day));
	                        selectedDate.set(Calendar.MONTH, month);
	                        selectedDate.set(Calendar.YEAR, year);
	                        displayDate();	
	                        
	                        if(fastMode == true) {
	                        	getDialogParent().setVisible(false);
	                        }
	                	}
	                }
                });
	        if (x < 7) {
                button[x].setText(header[x]);
                button[x].setForeground(Color.black);
	        }
	        p1.add(button[x]);
		}
		
		p1.setPreferredSize(new Dimension(430, 120));
		p2.add(previous);
		p2.add(l);
	    p2.add(next);
	    
	    add(p1, BorderLayout.CENTER);
	    add(p2, BorderLayout.SOUTH);
	    displayDate();
	}

    public void displayDate() {
    	int dayOfWeek = 0;
        int daysInMonth = 0;
        int selectedYear = selectedDate.get(Calendar.YEAR);
    	int selectedMonth = selectedDate.get(Calendar.MONTH);
    	int selectedDay = selectedDate.get(Calendar.DAY_OF_MONTH);
    	SimpleDateFormat sdf = new SimpleDateFormat("MMMM yyyy");
        Calendar cal = Calendar.getInstance();
        
        for (int x = 7; x < button.length; x++) {
        	button[x].setText("");
        }
        
        cal.set(year, month, 1);
        dayOfWeek = cal.get(java.util.Calendar.DAY_OF_WEEK);
        daysInMonth = cal.getActualMaximum(java.util.Calendar.DAY_OF_MONTH);
        
        for (int x = 6 + dayOfWeek, day = 1; day <= daysInMonth; x++, day++) {
        	button[x].setText("" + day);
        	
        	if(year == selectedYear && month == selectedMonth && day == selectedDay) {
        		button[x].setForeground(Color.red);
        	}
        	else {
        		button[x].setForeground(Color.black);
        	}
        }
        l.setText(sdf.format(cal.getTime()));
    }
    
    public String setPickedDate() {
        if (day.equals("")) {
        	return day;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
        Calendar cal = Calendar.getInstance();
        cal.set(year, month, Integer.parseInt(day));
        return sdf.format(cal.getTime());
    }
    
    public Date getSelectedDate() {
    	return selectedDate.getTime();
    }
    
    public JDialog getDialogParent() {
    	Container parent = getParent();
    	while(parent != null) {
    		if(parent instanceof JDialog) {
    			return (JDialog)parent;
    		}
    		parent = parent.getParent();
    	}
    	return null;
    }
}
